home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: Norman Bullen <nbullen@ix.netcom.com>
- Newsgroups: comp.lang.c
- Subject: Re: Ability to locate spaces?
- Date: Sat, 17 Feb 1996 15:44:05 -0800
- Organization: Black Cat Associates
- Message-ID: <31266845.2D65@ix.netcom.com>
- References: <Pine.SOL.3.91.960215222301.15979A-100000@teer1.acpub.duke.edu> <1996Feb16.162842.18380@zcon.com>
- NNTP-Posting-Host: ple-ca7-18.ix.netcom.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-NETCOM-Date: Sat Feb 17 4:01:10 PM PST 1996
- X-Mailer: Mozilla 2.0 (Win16; I)
-
- Syed Zaeem Hosain wrote:
- >
- > In article <Pine.SOL.3.91.960215222301.15979A-100000@teer1.acpub.duke.edu>, John
- Young Oh <jyo@acpub.duke.edu> writes:
- > >I am trying to write a program that reads an input file and locates where
- > >the spaces are located. For example, if a line from the input file read:
- > >555555.55 5555.55 5555.55
- > >
- > >I would like the program to be able to recognize that there are spaces
- > >between the numbers and keep track of them. What I did was to use
- > >fgets and read in the line into a string. I then used a for loop
- > >and checked each array element of the character string and used an
- > >if statement to see if an array element was a space.
- > >Here are the lines of code:
- > >
- > >FILE *input;
- > >char *string, buf[200];
- > >int i, count;
- > >input = fopen ("data", "r");
- > >(etc etc etc)
- > >string = fgets (buf, 199, input);
- > >for (i = 0; i < 40; ++i)
- > >{
- > > if (buf[i] == ' ')
- > > {
- > > ++count;
- > > }
- > >}
- > >
- > >What is wrong with this code? It does not seem to work.
- >
- > Well, what did the program do? What is the error? Is it a compiler
- > problem or a run-time problems? "Does not seem to work" is pretty darn
- > vague, in other words! :-)
- >
- > Here is what I would check:
- >
- > 1. Did you initialize count to 0 before the lines get read?
- >
- > 2. Did you check to see if fgets returns a legit result? I.e. the line
- > has something to read?
- >
- > 3. Is the line longer than 40 characters? If so, why does your loop
- > stop at 40 characters? Why not use the `\n` byte as a terminator for
- > the loop? Or the '\0' byte for that matter?
- >
- > 4. Are the lines longer than 199 characters? If so, why is your "buf"
- > size set to 200?
- >
- > 5. Did the call to "fopen" succeed? Are you reading real lines from
- > the input program?
- >
- > 6. Is the filename correct? Does the file called "data" exist?
- >
- > There may be other issues, but these are the ones that come to mind off
- > the top of my head since I do not know the symptoms you are actually
- > seeing when running the program.
- >
- > As an aside: your first sentence says "... locates where the spaces are
- > located". Later you say "recognize that there are spaces ... and keep
- > track of them". What are you really trying to do - simply count them or
- > something else?
- >
- > Z
- >
- > --
- > -------------------------------------------------------------------------> | Syed Zaeem Hosain P. O. Box 610097 (408) 441-7021 |
- > | Z Consulting Group San Jose, CA 95161 szh@zcon.com |
- > -------------------------------------------------------------------------
- The spaces that you see in the file may actually be tab characters or
- other "whitespace" characters. Instead of
- if (buf[i] == ' ')
- try
- if (isspace(buf[i]))
-
-